<!--
 * @Author: White
 * @description: Form组件
 * @LastEditTime: 2021-06-30 14:00:08
-->

<template>
    <!--是否行内表单-->
    <el-form :model="form" :ref="form" :rules="rules" label-width="100px">
        <!--标签显示名称-->
        <el-form-item v-for="item in formLabel" :key="item.model" :label="item.label" :prop="item.model">
            <!--根据type来显示是什么标签-->
            <el-input v-model="form[item.model]" :placeholder="'请输入' + item.label" v-if="item.type==='input'"></el-input>
            <el-select v-model="form[item.model]" placeholder="请选择" v-if="item.type === 'select'" clearable>
                 <!--如果是select或者checkbox 、Radio就还需要选项信息-->
                <el-option v-for="item in item.opts" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
            <el-switch v-model="form[item.model]" v-if="item.type === 'switch'"></el-switch>
            <el-date-picker v-model="form[item.model]" type="date" placeholder="选择日期" v-if="item.type === 'date'" value-format="yyyy-MM-dd"> </el-date-picker>
        </el-form-item>
        <!--留一个插槽-->
        <el-form-item>
            <el-button class="btn" type="primary" @click="submit(form)">提 交</el-button>
            <el-button class="btn" type="primary" @click="cancel">取 消</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        props: {
            form: Object,  //form表单数据 
            formLabel: Array,   //标签数据
            rules: Object   //表单规则
        },
        methods: {
            submit(form) {
                this.$refs[form].validate(valid => valid && this.$emit('submit', this.form));
            },
            cancel() {
                this.$emit('cancel', false)
            }
        }
    }
</script>